package com.shell.customer.accreditation.rx;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.shell.bee.base.utils.DateUtil;
import com.shell.bee.base.utils.SystemUtil;
import com.shell.customer.accreditation.AccreditEvent;
import com.shell.customer.base.service.CusOptService;
import com.shell.customer.po.CusOpt;
import com.shell.customer.po.CusRec;
import com.shell.framework.rx.OnErrorNotImplementedSubscriber;
import com.shell.framework.utils.CommonDataUtil;

@Component
public class SetCusOptSubscriber extends OnErrorNotImplementedSubscriber<AccreditEvent> {
	@Autowired
	private CusOptService cusOptService;

	public void onApplicationEvent(AccreditEvent event) {
		CusOpt cusOpt = event.getCusOpt();
		CusRec cus = event.getAccreditationVo().getCustomerC();
		cusOpt.setOptId(SystemUtil.getUUID());
		cusOpt.setCusId(cus.getCusId());

		CusOpt latestOpt = this.cusOptService.getLatestOpt(cus.getCusId());
		if (null != latestOpt) {
			cusOpt.setSeqNo(latestOpt.getSeqNo() + 1);
		} else {
			cusOpt.setSeqNo(0);
		}
		cusOpt.setOprId(CommonDataUtil.getUserId());
		cusOpt.setOprName(CommonDataUtil.getUserName());
		cusOpt.setOprTime(DateUtil.getSystemDateTime());
		this.cusOptService.save(cusOpt);
	}
}
